package com.example.demo.page.detail

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.demo.args.DemoArgs
import com.example.demo.args.DetailArgs
import com.example.demo.base.BaseBindingFragment
import com.example.demo.ext.observeWithLifecycle
import com.jetinno.coffee.databinding.FragmentLoginSuccessBinding
import timber.log.Timber

/**
 * 登录成功后展示的页面
 */
class LoginSuccessFragment :
    BaseBindingFragment<FragmentLoginSuccessBinding, LoginSuccessViewModel>() {
    override val viewModel: LoginSuccessViewModel by viewModels()
    // 自动获取传递的参数
//    private val args: DetailArgs by navArgs()
    override fun getViewBinding(): FragmentLoginSuccessBinding {
        return FragmentLoginSuccessBinding.inflate(layoutInflater)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.tvTitle.text = "登录成功!"
        binding.btnBack.setOnClickListener {
            findNavController().popBackStack()
        }
//        Timber.d("userId:${args.userId}")
    }

    override fun observeStateOrEvent(viewModel: LoginSuccessViewModel) {
        //界面状态
        viewModel.state.observeWithLifecycle(this) { state ->
            when (state) {
                LoginSuccessState.Back -> findNavController().navigate(DemoArgs)
                LoginSuccessState.Empty -> Timber.d("Empty")
            }
        }
        // 界面副作用
        viewModel.effect.observeWithLifecycle(this) { effect ->
            when (effect) {
                is LoginSuccessEvent.NavigationToHost -> {
                    Timber.d("跳转")
                }
            }
        }
    }


}